import {
  successResponse,
  errorResponse,
  validationErrorResponse,
} from "server/utils/response";
import { Projects } from "server/models/Project";

export default defineEventHandler(async (event) => {
  try {
    const { id } = getRouterParams(event);

    if (!id) {
      return validationErrorResponse("项目ID不能为空");
    }

    const projectId = parseInt(id);
    if (isNaN(projectId)) {
      return validationErrorResponse("项目ID格式不正确");
    }

    // 使用数据库删除
    const deleted = await Projects.deleteProject(projectId);

    if (!deleted) {
      return errorResponse("项目不存在或删除失败");
    }

    return successResponse(null, "项目删除成功");
  } catch (error) {
    console.error("删除项目失败:", error);
    return errorResponse(
      "删除项目失败",
      error instanceof Error ? error.message : "未知错误"
    );
  }
});
